package com.hewang.helloandroid.learnwebview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.hewang.helloandroid.R;

public class WebviewJsActivity extends AppCompatActivity implements JsBridgeWebview{
    private static final String TAG = "WebviewJsActivity";
    private Handler mHandler;
    private TextView mTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview_js);
        Log.d(TAG, "onCreate");
        mHandler = new Handler();
        mTextView = findViewById(R.id.webview_js_text);
        WebView myWebView = (WebView) findViewById(R.id.webview_js1);
        myWebView.getSettings().setJavaScriptEnabled(true); // 设置启用JavaScript
        myWebView.setWebViewClient(new WebViewClient()); // 设置在webview内打开网页中的页面链接，而不是使用系统网络浏览器打开
        myWebView.loadUrl("http://192.168.0.103:8080/01_hellowebview.html");
        myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
    }
     public void setTextViewValue(String value) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                mTextView.setText(value);
            }
        });
     }
}